package cn.gdcp.lkqapp11;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private MapView mapView;
    private BaiduMap baiduMap;
    private LocationClient mLocationClient = null;
    private MyLocationConfiguration myLocationConfiguration;
    private RadioGroup radioGroup;
    private RadioButton rb1;
    private RadioButton rb2;
    private CheckBox cb1;
    private CheckBox cb2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取地图控件引用
        mapView = findViewById(R.id.bmapView);
        radioGroup = findViewById(R.id.rdg1);
        rb1 = findViewById(R.id.rdb1);
        rb2 = findViewById(R.id.rdb2);
        cb1 = findViewById(R.id.cb1);
        cb2 = findViewById(R.id.cb2);

        baiduMap = mapView.getMap();
        baiduMap.setMyLocationEnabled(true);

        //地图Logo
        mapView.setLogoPosition(LogoPosition.logoPostionRightTop);

        //指南针
        //实例化UiSettings类对象
        UiSettings uiSettings = baiduMap.getUiSettings();
        //通过设置enable为true或false 选择是否显示指南针
        uiSettings.setCompassEnabled(true);

        // 比例尺
        mapView.showScaleControl(true);
        // 缩放按钮
        mapView.showZoomControls(true);

        //隐藏地图标注，只显示道路信息
        //默认显示地图标注
        baiduMap.showMapPoi(true);

        //直接缩放至缩放级别14
        baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(14));

        //长按
        baiduMap.setOnMapLongClickListener(new BaiduMap.OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng latLng) {
                //构建Marker图标
                BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.marker);
                //构建MarkerOption,用于地图上添加marker
                OverlayOptions options = new MarkerOptions()
                        .position(latLng)
                        .icon(bitmapDescriptor);
                //显示marker
                baiduMap.addOverlay(options);
            }
        });

        //单击
        baiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Toast.makeText(MainActivity.this,"单击的坐标：纬度：" + latLng.latitude + "，经度：" + latLng.longitude,Toast.LENGTH_SHORT).show();
                //构建折线点坐标
                LatLng p1 = latLng;
                LatLng p2 = new LatLng(p1.latitude-0.002, p1.longitude);
                LatLng p3 = new LatLng(p1.latitude-0.002, p1.longitude + 0.002);
                LatLng p4 = new LatLng(p1.latitude, p1.longitude + 0.002);
                List<LatLng> points = new ArrayList<LatLng>();
                points.add(p1);
                points.add(p2);
                points.add(p3);
                points.add(p4);
                points.add(p1);

                //设置折线的属性
                OverlayOptions overlayOptions = new PolylineOptions()
                        .width(5)
                        .color((0xAAFF0000))
                        .points(points);
                //绘制折线
                Overlay polyline = baiduMap.addOverlay(overlayOptions);

            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });

        //双击
        baiduMap.setOnMapDoubleClickListener(new BaiduMap.OnMapDoubleClickListener() {
            @Override
            public void onMapDoubleClick(LatLng latLng) {
                Toast.makeText(MainActivity.this,"单击的坐标：纬度：" + latLng.latitude + "，经度：" + latLng.longitude,Toast.LENGTH_SHORT).show();

                /*showInfoWindow(latLng);*/
                /*drawText(latLng);*/
                drawCircle(latLng);

            }
        });

        //中心
        initialCenter();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == rb1.getId()) {
                    //普通地图
                    baiduMap.setMyLocationEnabled(true);
                    baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                } else if (checkedId == rb2.getId()) {
                    //卫星地图
                    baiduMap.setMyLocationEnabled(true);
                    baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                }

            }
        });

        cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //开启交通路况图
                    baiduMap.setTrafficEnabled(true);
                } else {
                    baiduMap.setTrafficEnabled(false);
                }
            }
        });

        cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //开启城市热力图
                    baiduMap.setBaiduHeatMapEnabled(true);
                } else {
                    baiduMap.setBaiduHeatMapEnabled(false);
                }
            }
        });

        baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(MainActivity.this, "markerHavaClick......",Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        //定位
        initialLocation();

    }

    private void drawCircle(LatLng latLng) {
        //圆心位置
        LatLng center = latLng;

        //构造CircleOptions对象
        CircleOptions mCircleOptions = new CircleOptions().center(center)
                .radius(200)
                .fillColor(0xAAFFFF88) //填充颜色
                .stroke(new Stroke(5, 0xAA00ff00)); //边框宽和边框颜色

        //在地图上显示圆
        Overlay mapCircle = baiduMap.addOverlay(mCircleOptions);
    }

    private void drawText(LatLng latLng) {
        //文字覆盖位置坐标
        LatLng latLngText = latLng;

        //构建TextOptions对象
        OverlayOptions TextOptions = new TextOptions()
                .text("百度地图SDK") //文字内容
                .bgColor(0xAAFFFF00) //背景色
                .fontSize(28) //字号
                .fontColor(0xFFFF00FF) //文字颜色
                .rotate(-90) //旋转角度
                .position(latLngText);

        //显示文字
        Overlay mapText = baiduMap.addOverlay(TextOptions);
    }

    private void showInfoWindow(LatLng latLng) {
        //用来构造InfoWindow的button
        Button button = new Button(getApplicationContext());
        button.setBackgroundResource(R.drawable.redbg);
        button.setText("广州发电ing");

        //构造InfoWindow
        //point 描述的位置点
        //-100 InfoWindow相对于point在y轴的偏移量
        InfoWindow mInfoWindow = new InfoWindow(button, latLng, -100);

        //使InfoWindow生效
        baiduMap.showInfoWindow(mInfoWindow);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mLocationClient.stop();
        baiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView = null;
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mapView == null) {
                return;
            }


            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(200)
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            baiduMap.setMyLocationData(locData);
        }
    }

    private void initialCenter() {
        LatLng cenpt = new LatLng(23.188974, 113.35965);
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(18)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化


        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        baiduMap.setMapStatus(mMapStatusUpdate);
    }

    private void initialLocation(){
        //定位初始化
        mLocationClient = new LocationClient(this);

        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);

        //设置locationClientOption
        mLocationClient.setLocOption(option);

        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
        //开启地图定位图层
        mLocationClient.start();

        myLocationConfiguration = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,
                false, BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher), 0xAAFFFF88, 0xAA00FF00);
        baiduMap.setMyLocationConfiguration(myLocationConfiguration);
    }


}
